!function () {
    layui.config({base: '/layui/lay/modules/'}).use(['upload','laydate',  'form','status','common'], function () {
        //定义
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload,
            laydate = layui.laydate,
            layuiSta = layui.status,//自定义状态控件
            common = layui.common;//自定义状态控件

        //启动富文本编辑
        var ueditor ;
        //初始化渲染列表
        layuiSta.initSta("#statusDiv",'1',true);//构建状态
        common.initMenusOptions("#type","brand");//构建下拉选项
        //初始化表单
        laydate.render({
            elem: '#releaseTime',
            type: 'datetime'
        });

        //清空富文本编辑框
        ueditor = UE.getEditor('ueditorText');
        $('.edui-editor-bottomContainer').hide();
        //普通图片上传
        var uploadFileBtn = upload.render({
            elem: '#uploadBtn'
            ,url: '/upload/uploadFiles'
            ,field:"layuiFiles"
            ,accept: 'file' //普通文件
            ,auto: false    //不自动上传
            ,exts: 'jpg|png|jpeg' //只允许上传压缩文件
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    var $uploadListDiv = $('#uploadListDiv');
                    var $brandType = $("#type").val();
                    var htmlDiv ='';
                    if(file.type.indexOf("image") !=-1){
                        if( (file.size/1014).toFixed(1) > 10240 ){
                            return layer.msg("图片不能超过10M");
                            return false;
                        }
                        //限制图片尺寸
                        var img = new Image();

                        var flagUpload = true;
                        img.onload = function(){
                            if(($brandType ==11|| $brandType== 12)){
                                if( img.width != 870 || img.height != 700){
                                    layer.msg("品牌历史与荣誉图片固定尺寸为870*700");
                                    flagUpload= false;
                                }
                            }else{
                                if(img.width != 380 || img.height != 320){
                                    layer.msg("品牌图片固定尺寸为380*320");
                                    flagUpload= false;
                                }
                            }
                            uploadFileBtn.config.data ={"dir":"image"};
                            htmlDiv +='<img id="fileDiv" style="height: 137px;width: 137px;"/>';
                            if( flagUpload ){
                                $uploadListDiv.empty().append(htmlDiv);
                                //执行上传
                                obj.upload(index, file);
                            }
                        };
                        img.src = result;
                    }
                });
            }
            ,done: function(res){
                if(res.code==1){
                    layer.alert(res.message,5);
                }
                if(res.error>0){
                    return layer.msg(res.message);
                }
                if(res.error==0){
                    var $uploadListDiv = $('#uploadListDiv');
                    $("#url").val(res.url);
                    $uploadListDiv.find("#fileDiv").attr("src",res.url);
                    layer.msg("上传成功");
                }
            }
        });

        var uploadAdvertFileBtn = upload.render({
            elem: '#uploadAdvertBtn'
            ,url: '/upload/uploadFiles'
            ,field:"layuiFiles"
            ,accept: 'file' //普通文件
            ,auto: false    //不自动上传
            ,exts: 'jpg|png|jpeg' //只允许上传压缩文件
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    var $uploadListDiv = $('#uploadAdvertListDiv');
                    var htmlDiv ='';
                    if(file.type.indexOf("image") !=-1){
                        if( (file.size/1014).toFixed(1) > 10240 ){
                            return layer.msg("图片不能超过10M");
                            return false;
                        }
                        //限制图片尺寸
                        var img = new Image();

                        var flagUpload = true;
                        img.onload = function(){
                            /*if(img.width != 380 || img.height != 320){
                                layer.msg("品牌图片固定尺寸为380*320");
                                flagUpload= false;
                            }*/
                            uploadFileBtn.config.data ={"dir":"image"};
                            htmlDiv +='<img class="fileDiv" style="height: 137px;width: 137px;"/>';
                            if( flagUpload ){
                                $uploadListDiv.empty().append(htmlDiv);
                                //执行上传
                                obj.upload(index, file);
                            }
                            $(".upload-del-icon").show();
                        };
                        img.src = result;
                    }
                });
            }
            ,done: function(res){
                if(res.code==1){
                    layer.alert(res.message,5);
                }
                if(res.error>0){
                    return layer.msg(res.message);
                }
                if(res.error==0){
                    var $uploadListDiv = $('#uploadAdvertListDiv');
                    $("#advertUrl").val(res.url);
                    $uploadListDiv.find(".fileDiv").attr("src",res.url);
                    layer.msg("上传成功");
                }
            }
        });

        $('#backRecord').click(function(){
            window.location.href="/brand/list";
        });

        //删除浮动广告操作
        $('.upload-del-icon').click(function(){
            $('#advertUrl').val('');
            $("#uploadAdvertListDiv").empty();
            $(this).addClass("div-hide");
        });
        //自定义验证规则
        form.verify({
            nameLen: [
                /^[\d\D]{1,32}$/
                ,'最多输入32个字'
            ]
        });

        //监听提交事件
        form.on('submit(editRecord)',function(data){

            //获取富文本编辑内容
            var brandText = ueditor.getContent();
            data.field['brandText']=brandText;
            var postUrl = !data.field['brandId'] ? '/brand/addBrand' : '/brand/editBrand';
            if( data.field.status == "undefined" || data.field.status == null){
                layer.msg("请选择状态");
                return false;
            }
            $.ajax({
                async: false, type: 'post',
                traditional: true,
                data: JSON.stringify(data.field),
                contentType:"application/json;charset=UTF-8",
                processData:false,
                url: postUrl,
                success: function (data) {
                    if (data && data.code == 200) {
                        layer.closeAll();
                        layer.alert('操作成功！', {icon: 1, title: '提示',yes:function(){
                                window.location.href="/brand/list";
                            }});
                    } else
                        layer.alert(data.msg || '操作失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.closeAll();
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
            return false;
        });

    });
}();